package com.dlws.modules.price.controller;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.dlws.common.web.BaseController;
import com.dlws.modules.field.entity.FieldPrice;
import com.dlws.modules.field.entity.ProductField;
import com.dlws.modules.price.entity.Price;
import com.dlws.modules.price.entity.PriceVo;
import com.dlws.modules.price.service.PriceService;

/**
 * 
 * 描述：
 * 创建人： 魏泽超
 * 创建时间: 2016-6-21 上午9:47:15
 * @version
 */
@Controller
@RequestMapping("/price")
public class PriceController extends BaseController {

	@Autowired
	private PriceService priceService;

	/**
	 * 
	 * 描述：设置价格
	 * 创建人： 魏泽超
	 * 创建时间: 2016-6-21 上午10:29:59
	 * @version
	 */
	@ResponseBody
	@RequestMapping("/add")
	public Map<String, Object> addPrice(PriceVo priceVo) {
		logger.info("进入添加价格表方法-->");
		Map<String, Object> map = new HashMap<String, Object>();
		Price p = new Price();
		p.setProductId(priceVo.getProductId());
		p.setIsDelete(0);
		p.setIsUse(1);
		List<Price> pAll = priceService.queryAll(p);
		boolean flag=true;
		if (null != pAll && pAll.size() > 0) {
			flag = toMap(priceVo.getField(), pAll);
		}
		if(flag){
			Price price = new Price();
			price.setField(priceVo.getField());
			price.setIsDelete(0);
			price.setIsUse(1);
			price.setPrice(priceVo.getPrice());
			price.setProductId(priceVo.getProductId());
			price.setProductName(priceVo.getProductName());
			int i = priceService.add(price);
			if (i > 0) {
				map.put("status", true);
				map.put("msg", "价格设置成功");
			} else {
				map.put("status", false);
				map.put("msg", "价格设置失败");
			}
		}else{
			map.put("status", false);
			map.put("msg", "已经存在该组合的价格设置,请先删除旧的组合");
		}
		
		return map;
	}

	private boolean toMap(String json, List<Price> pAll) {
		// JSON -> Map
		boolean flag = true;
		// 提交的json
		LinkedHashMap<String, Object> subJsonMap = JSON.parseObject(json, new TypeReference<LinkedHashMap<String, Object>>() {});
		LinkedHashMap<String, Object> subJsonNewMap = new LinkedHashMap<String, Object>();
		for (String key : subJsonMap.keySet()) {
			if (key.equals("productId") || key.equals("productName") || key.equals("price")) {
				continue;
			}
			subJsonNewMap.put(key, subJsonMap.get(key));
		}
		for (Price price : pAll) {
			LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
			LinkedHashMap<String, Object> entityMap = JSON.parseObject(price.getField(), new TypeReference<LinkedHashMap<String, Object>>() {
			});
			for (String key : entityMap.keySet()) {
				if (key.equals("productId") || key.equals("productName") || key.equals("price")) {
					continue;
				}
				map.put(key, entityMap.get(key));
			}
			if (subJsonNewMap.equals(map)) {
				flag = false;
			}
		}
		return flag;
	}
	/**
	 * 
	 * 描述：删除状态更新
	 * 创建人： 王小雷
	 * 创建时间: 2016-6-27 下午4:50:41
	 * @version
	 */
	@RequestMapping("/delUpdate")
	public String delUpdate(Price price,ModelMap model){
		
		Price queryOne= priceService.queryOne(price);
		queryOne.getIsDelete();
		if(queryOne.getIsDelete()==0){
			queryOne.setIsDelete(1);
		}else {
			queryOne.setIsDelete(0);
		}
		priceService.updateByPrimaryKeySelective(queryOne);
	return "redirect:/template/toAddPrice.do?productId="+queryOne.getProductId();
	}
	/**
	 * 
	 * 描述：启用状态更新
	 * 创建人： 王小雷
	 * 创建时间: 2016-6-27 下午4:51:04
	 * @version
	 */
	@RequestMapping("/qiyongUpdate")
	public String qiyongUpdate(Price price,ModelMap model){
		Price queryOne= priceService.queryOne(price);
		queryOne.getIsUse();
		if(queryOne.getIsUse()==1){
			queryOne.setIsUse(0);
		}else {
			queryOne.setIsUse(1);
		}
		priceService.updateByPrimaryKeySelective(queryOne);
	return "redirect:/template/toAddPrice.do?productId="+queryOne.getProductId();
	}
}
